﻿// -----------------------------------------------------------------------
// <copyright file="EntityBase.cs" company="Spardo, LLC">
// Copyright (c) 2011 SPardo, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the 
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
// </copyright>
// -----------------------------------------------------------------------

namespace DotNet37Signals.Client
{
    using System.Net;
    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    internal class RestClientBase<TChannel> : ClientBase<TChannel> where TChannel : class, IContract
    {
        public RestClientBase(Credentials credentials
            , string hostName
            , bool basicAuthentication)
            : this(new ConnectionSettings(credentials, hostName, basicAuthentication)) 
        {
        }
        
        private RestClientBase(ConnectionSettings configuration)
            : base(CreatePOXBinding(configuration), new EndpointAddress(configuration.GetBaseUrl()))
        {
            var binding = base.Endpoint.Binding as CustomBinding;
            var element = binding.Elements.Find<WebMessageEncodingBindingElement>();
            var mapper = element.ContentTypeMapper as RestWebContentTypeMapper;

            base.Endpoint.Behaviors.Add(new RestHttpBehavior(mapper, configuration));

            base.ClientCredentials.UserName.UserName = configuration.Credentials.Username;
            base.ClientCredentials.UserName.Password = configuration.Credentials.Password;
        }

        public TChannel GetContract()
        {
            return base.Channel;
        }

        private static Binding CreatePOXBinding(ConnectionSettings configuration)
        {
            var message = new WebMessageEncodingBindingElement(Encoding.UTF8)
            {
                MessageVersion = MessageVersion.None,
                ContentTypeMapper = new RestWebContentTypeMapper()
            };

            var transport = new HttpsTransportBindingElement
            {
                ManualAddressing = true,
                KeepAliveEnabled = configuration.Credentials.KeepAliveEnabled,
                MaxReceivedMessageSize = configuration.Credentials.MaxReceivedMessageSize,
                AuthenticationScheme = (configuration.BasicAuthenticationEnabled) ? AuthenticationSchemes.Basic : AuthenticationSchemes.Anonymous
            };
            
            var output = new CustomBinding(message, transport);

            return output;
        }
    }
}
